package com.wxb.qd.exception;

import com.wxb.qd.common.BaseResponse;
import com.wxb.qd.common.ErrorCode;
import com.wxb.qd.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author wangxb
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 异常捕捉，打印异常信息
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e){
        log.error("businessException :" , e);
        return ResultUtils.error(e.getCode(),e.getMessage());
    }

    /**
     * 运行时异常处理，捕捉到打印异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }

}
